/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-26
*/
package com.rzico.basics.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.*;
import com.rzico.basics.model.ProductVo;
import com.rzico.basics.model.ShopVo;
import com.rzico.basics.service.*;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收藏控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-05-26
 */
@Api(description = "收藏店铺")
@RestController
@RequestMapping("/member/shopFavorites")
public class ShopFavoritesMemberController extends BaseController {

    @Autowired
    private ShopFavoritesService shopFavoritesService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ShopService shopService;

    /**
     * 分页查询收藏
     *
     * @return
    */
    @ApiOperation("分页查询收藏")
    @GetMapping("/list")
    public CommResult<ProductVo> list(Long memberId,Double lat, Double lng, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);


        Member member = memberService.getCurrent();

        if (member!=null) {
            params.put("memberId",member.getId());
        }

        Double latU = null;
        Double lngU = null;
        if (lat != null && lng != null) {
            latU = lat;
            lngU = lng;
        }

        List<ShopVo> list = shopFavoritesService.selectShopVoList(params,latU,lngU);


        Integer startRow = (pageable.getPageNum()-1)*pageable.getPageSize();
        Integer endRow = startRow+pageable.getPageSize();
        if (endRow>list.size()) {
            endRow = list.size();
        }

        PageResult<ShopVo> pageResult = new PageResult<ShopVo>(list.subList(startRow,endRow), list.size(), pageable);

        return CommResult.success(pageResult);
    }

    /**
     * 获取收藏商品数量
     *
     * @return
     */
    @ApiOperation("获取收藏商品数量")
    @GetMapping("/count")
    public CommResult<Long> count() {
        Member member = memberService.getCurrent();
        Map<String,Object> params = new HashMap<>();
        params.put("memberId",member.getId());
        //获取收藏
        Integer fav = shopFavoritesService.selectRowCount(params);
        return CommResult.success(fav);

    }


    /**
     * 检是是否有收藏
     *
     * @return
     */
    @ApiOperation("检是是否有收藏")
    @GetMapping("/exists")
    public CommResult<Long> exists(Long shopId) {
        Member member = memberService.getCurrent();
        Integer fav = 0;
        if (member==null) {
            return CommResult.success(fav);
        }
        Map<String,Object> params = new HashMap<>();
        params.put("memberId",member.getId());
        params.put("shopId",shopId);
        //获取收藏
        fav = shopFavoritesService.selectRowCount(params);
        return CommResult.success(fav);

    }

    /**
     * 添加收藏
     *
     * @param shopFavorites
     * @return
     */
    @ApiOperation("添加收藏")
    @PostMapping("/add")
    public CommResult<ShopFavorites> add(@RequestBody ShopFavorites shopFavorites) {
        Member member = memberService.getCurrent();
        Shop shop = shopService.findById(shopFavorites.getShopId());
        shopFavorites.setCreateDate(new Date());
        shopFavorites.setModifyDate(new Date());
        shopFavorites.setMemberId(member.getId());
        shopFavorites.setShopId(shop.getId());
        int affectCount = shopFavoritesService.insertUseGeneratedKeys(shopFavorites);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        return CommResult.success("收藏成功");

    }


    /**
     * 批量删除收藏
     *
     * @return
     */
    @Log(desc = "批量删除收藏", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除收藏,ids用逗号拼接")
    @PostMapping("/del")
    @ApiImplicitParams({@ApiImplicitParam(name = "shopId", value = "店铺ID", dataType = "String", paramType = "query")
    })
    public CommResult<ShopFavorites> del(Long shopId) {

        Member member = memberService.getCurrent();

        ShopFavorites shopFavorites = new ShopFavorites();
        shopFavorites.setShopId(shopId);
        shopFavorites.setMemberId(member.getId());

        shopFavoritesService.delete(shopFavorites);
        return CommResult.success("取消收藏");
    }


}
